#import "UIVCLive.h"
#import "rc_control.h"

@implementation UIVCLive

- (void)viewDidLoad
{
    [super viewDidLoad];

    /* selfview */
    self.title = @"Live";
    self.view.backgroundColor = [UIColor grayColor];

    /* subviews */
    _labTips = [[UILabel alloc] init];
    _labTips.translatesAutoresizingMaskIntoConstraints = NO;
    _labTips.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:_labTips];
    
    _indicatorView = [[UIActivityIndicatorView alloc] init];
    _indicatorView.translatesAutoresizingMaskIntoConstraints = NO;
    [self.view addSubview:_indicatorView];
    
    [self constraintsWithLandscape];
    
    // 鼠标左键单击
    mGestureForClick = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGestureForClick:)];
    mGestureForClick.numberOfTapsRequired = 1;
    mGestureForClick.numberOfTouchesRequired = 1;
    [self.view addGestureRecognizer:mGestureForClick];
    
    // 鼠标左键双击
    mGestureForDbClick = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleGestureForDbClick:)];
    mGestureForDbClick.numberOfTapsRequired = 2;
    mGestureForDbClick.numberOfTouchesRequired = 1;
    [self.view addGestureRecognizer:mGestureForDbClick];
    
    // 鼠标左键拖拽
    mGestureForDrag = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleGestureForDrag:)];
    mGestureForDrag.numberOfTouchesRequired = 1;
    [self.view addGestureRecognizer:mGestureForDrag];

    // 缩放屏幕
    mGestureForZoom = [[UIPinchGestureRecognizer alloc] initWithTarget:self action:@selector(handleGestureForZoom:)];
    [self.view addGestureRecognizer:mGestureForZoom];
    
    // 返回参数设置
    mGestureForGotoPrev = [[UISwipeGestureRecognizer alloc] initWithTarget:self action:@selector(handleGestureForGotoPrev:)];
    mGestureForGotoPrev.direction = UISwipeGestureRecognizerDirectionLeft;
    mGestureForGotoPrev.numberOfTouchesRequired = 2;
    [self.view addGestureRecognizer:mGestureForGotoPrev];
}

- (void)constraintsWithLandscape
{
    if (self.view.constraints) [self.view removeConstraints:self.view.constraints];
    
    NSArray* constraints = [[NSArray alloc] initWithObjects:
                            
                            [NSLayoutConstraint constraintWithItem:_labTips       attribute:NSLayoutAttributeLeft    relatedBy:NSLayoutRelationEqual toItem:self.view    attribute:NSLayoutAttributeLeft             multiplier:1.0 constant:  0.0],
                            [NSLayoutConstraint constraintWithItem:_labTips       attribute:NSLayoutAttributeRight   relatedBy:NSLayoutRelationEqual toItem:self.view    attribute:NSLayoutAttributeRight            multiplier:1.0 constant:  0.0],
                            [NSLayoutConstraint constraintWithItem:_labTips       attribute:NSLayoutAttributeHeight  relatedBy:NSLayoutRelationEqual toItem:nil          attribute:NSLayoutAttributeNotAnAttribute   multiplier:1.0 constant: 30.0],
                            [NSLayoutConstraint constraintWithItem:_labTips       attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:self.view    attribute:NSLayoutAttributeCenterY          multiplier:1.0 constant:  0.0],
                            
                            [NSLayoutConstraint constraintWithItem:_indicatorView attribute:NSLayoutAttributeCenterX relatedBy:NSLayoutRelationEqual toItem:_labTips     attribute:NSLayoutAttributeCenterX          multiplier:1.0 constant:  0.0],
                            [NSLayoutConstraint constraintWithItem:_indicatorView attribute:NSLayoutAttributeCenterY relatedBy:NSLayoutRelationEqual toItem:_labTips     attribute:NSLayoutAttributeCenterY          multiplier:1.0 constant:  0.0],
                            
                            nil];
    
    [self.view addConstraints:constraints];
}

- (void)viewWillAppear:(BOOL)animated
{
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

- (void)viewDidAppear:(BOOL)animated
{
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, NSEC_PER_SEC * 1.0), dispatch_get_main_queue(), ^{
        
        [_indicatorView startAnimating];
        
        dispatch_queue_t queue = dispatch_queue_create("connectToTarget", NULL);
        dispatch_async(queue, ^{
            
            int ret = [self connectToTarget];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                [_indicatorView stopAnimating];
                
                if (ret < 0)
                {
                    _labTips.text = @"load failed!";
                }
                else
                {
                    _labTips.text = @"";
                }
            });
        });

    });
}

- (void)handleGestureForClick:(UITapGestureRecognizer*)gesture
{
    NSLog(@"%s", __FUNCTION__);
}

- (void)handleGestureForDbClick:(UITapGestureRecognizer*)gesture
{
    NSLog(@"%s", __FUNCTION__);
}

- (void)handleGestureForDrag:(UILongPressGestureRecognizer*)gesture
{
    NSLog(@"%s", __FUNCTION__);
}

- (void)handleGestureForZoom:(UIPinchGestureRecognizer*)gesture
{
    NSLog(@"%s", __FUNCTION__);
}

- (void)handleGestureForGotoPrev:(UISwipeGestureRecognizer*)gesture
{
    CATransition* transition = [CATransition animation];
    transition.duration = 0.5;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = @"oglFlip";
    transition.subtype = kCATransitionFromTop;
    transition.delegate = self;
    [self.navigationController.view.layer addAnimation:transition forKey:@"animationxxxx"];
    
    [self.navigationController popViewControllerAnimated:NO];
}

- (int)connectToTarget
{
    AppVariables* variables = [AppVariables variables];
    NSString* targetIP = variables.targetIP;
    NSString* password = variables.password;
    
    int ret;
    
    ret = rc_shut_device(_rcHandle);
    
    ret = rc_open_device([targetIP UTF8String], 3838, [password UTF8String], &_rcHandle);
    
    return ret;
}

@end
